<?php
/**
 * 
 * @todo description of this class
 * 
 * @author Alton Crossley <crossleyframework@nogahidebootstrap.com>
 * @package Crossley Framework
 *  
 * @copyright Copyright (c) 2003-2009, Nogaide BootStrap INC. All rights reserved.
 * @license BSD http://opensource.org/licenses/bsd-license.php
 * @version $Id:$
 * 
 */

/**
 * @namespace
 */
namespace X\Filter;
use X\Filter;

class Simple implements Filter
{
    /**
     * if white space is allowed
     *
     * @var boolean
     */
    public $AllowWhiteSpace;
    
    /**
     * permited characters
     *
     * @var array
     */
    public $PermittedCharacters;
    
    /**
     * Sets default option values for this instance
     *
     * @param  boolean $allowWhiteSpace
     * @return void
     */
    public function __construct($AllowWhiteSpace = false, Array $Permitted = array())
    {
        $this->AllowWhiteSpace = (boolean) $AllowWhiteSpace;
        $this->PermittedCharacters = $Permitted;
    }
    
    /**
     * Returns the string $value, removing all but alphabetic and digit characters
     * Patterned after Zend Filter Alnum
     *
     * @param  string|array $Subject
     * @return string|array (same as passed in)
     */
    public function filter($Subject)
    {
        $WhiteSpace = ($this->AllowWhiteSpace) ? '\s' : '';
        
        $ExtraChars = (empty($this->PermittedCharacters)) ? '' : trim(implode('', $this->PermittedCharacters));
        
        $Pattern = '/[^a-zA-Z0-9' . $WhiteSpace . $ExtraChars . ']/u';
        
        return preg_replace($Pattern, '', (string) $Subject);
    }
    
    /**
     * Allow for static execution
     * 
     * @param string|array $Subject
     * @param bool $AllowWhiteSpace
     * @param array $Permitted
     * @return string|array
     */
    static function process($Subject, $AllowWhiteSpace = false, Array $Permitted = array())
    {
        $Self = __CLASS__;
        $Fileter = new $Self($AllowWhiteSpace, $Permitted);
        
        if (is_array($Subject))
        {
            $aFiltered = array();
            foreach ($Subject as $sKey => $Value)
            {
                $aFiltered[$sKey] = $Fileter->filter((string)$Value);
            }
            return $aFiltered;
        }
        else
        {
            return $Fileter->filter((string)$Subject);
        }
        
    }
    
    /**
     * allow object to be invoked
     * 
     * @param string|array $Subject
     * @return string|array
     */
    public function __invoke($Subject)
    {
    	return $this->filter($value);
    }
}
